files <- list.files(path = "Data/Nationscape", pattern = "\\.dta$", full.names = TRUE)

vars <- c("response_id", "start_date", "group_favorability_democrats", "group_favorability_republicans", "pid3", "education", "race_ethnicity", 'gender', 'weight', 'state')

for (i in seq_along(files)) {
  assign(paste("Df", i, sep = "."), read_dta(files[i]))
}

for (i in seq_along(files)) {
  assign(paste("Df", i, sep = "."),
         select(get(paste("Df", i, sep = ".")), vars))
}

#combine all dataframes into one
df_ns <- ldply(mget(ls(pattern = "Df")), data.frame)

# recode
df1_ns <- df_ns %>% mutate(date = as.Date(start_date, format = "%Y-%m-%d")) %>% 
  mutate(pid = case_when(pid3 == 1 ~ "Democrat",
                         pid3 == 2 ~ "Republican",
                         pid3 == 3 ~ "Independent",
                         TRUE ~ "Other/Don't Know/Refused")) %>% 
  filter(pid != "Other/Don't Know/Refused", pid != "Independent") %>% 
  mutate(educ4 = case_when(
    education %in% c(1, 2, 3) ~ 1,
    education %in% c(4,5,6) ~ 2,
    education %in% c(7,8,9) ~ 3,
    education %in% c(10,22) ~ 4)) %>% 
  mutate(race = case_when(
    race_ethnicity %in% c(1) ~ 1,
    race_ethnicity %in% c(2) ~ 2,
    race_ethnicity %in% c(3) ~ 3,
    race_ethnicity %in% c(4:10) ~ 4,
    race_ethnicity %in% c(11:14) ~ 5,
    race_ethnicity %in% c(15) ~ 6)) %>% 
  filter(group_favorability_republicans != 999, group_favorability_democrats != 999) %>%
  mutate(group_favorability_republicans = 5-group_favorability_republicans,
         group_favorability_democrats = 5-group_favorability_democrats,
         infeels = case_when(
           pid == "Republican" ~ group_favorability_republicans,
           pid == "Democrat" ~ group_favorability_democrats),
         outfeels = case_when(
           pid == "Republican" ~ group_favorability_democrats,
           pid == "Democrat" ~ group_favorability_republicans),
         affpol = infeels - outfeels)

df2_ns <- df1_ns %>% mutate(election_day = as.Date('2020-11-03', format = "%Y-%m-%d"),
                      distance_comp = difftime(election_day, date, units = 'days'),
                      distance_comp = as.numeric(distance_comp),
                      days = distance_comp*-1,
                      treatment = ifelse(days <= -1, 0, 1))

final_ns <- df2_ns %>% mutate(race = as.factor(race),
                        educ4 = as.factor(educ4),
                        gender = as.factor(gender))

y_affpol <- final_ns$affpol
x_1 <- final_ns$days

rdrobust(y_affpol, x_1, c = 0)

stan_ns <- final_ns %>% filter(days >= -26 & days <= 26) %>% 
  mutate(days_scaled = as.numeric(scale(days)),
         affpol_scaled = as.numeric(scale(affpol)))

m1_ns <- lm(affpol_scaled ~ days_scaled * treatment, stan_ns, weights = weight)


m1a_ns <- lm(affpol_scaled ~ days_scaled * treatment + race+educ4+gender, stan_ns, weights = weight)


stargazer::stargazer(m1_ns,m1a_ns, type = 'latex',
                     title="Nationscape Results",
                     omit.stat = c("ser", "adj.rsq", 'rsq'), 
                     font.size = "scriptsize", column.sep.width = "6pt",
                     df = F, no.space = T,
                     dep.var.labels = c(
                       "Aff Pol"))